// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SongEditor.java

package presenter;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

// Referenced classes of package presenter:
//            SongFile

public class SongEditor extends JPanel implements ActionListener {

    private SongFile song;
    private JTextArea text;
    private JFrame jf;
    JButton save;
    JButton saveAs;
    JPanel bottom;
    JLabel ccliNumber;
    JTextField number;
    JPanel split;
    JScrollPane scrollPane;
    JLabel songTitle;
    JTextField title;
    
    public SongEditor() {
        super(new BorderLayout());
        jf = new JFrame("Song Editor");
        jf.setSize(640, 480);
        jf.setLocation(150, 150);
    }
    
    public SongEditor(SongChooser sc) {
        this(new SongFile(sc.getNumber()));

    }

    public SongEditor(SongFile song) {
        this();
        setSong(song);
        save = new JButton("Save");
        saveAs = new JButton("Save As");
        ccliNumber = new JLabel("CCLI Number");
        number = new JTextField();
        number.setText((new StringBuilder()).append("").append(song.getNumber()).toString());
        split = new JPanel(new GridLayout(2, 2));
        bottom = new JPanel(new GridLayout(1, 2));
        songTitle = new JLabel("Song Name");
        title = new JTextField();
        title.setText((new StringBuilder()).append("").append(song).toString());
        split.add(songTitle);
        split.add(title);
        split.add(ccliNumber);
        split.add(number);
        add(split, "North");
        scrollPane = new JScrollPane();
        scrollPane.getViewport().setView(text);
        scrollPane.setVisible(true);
        add(scrollPane, "Center");
        bottom.add(save);
        bottom.add(saveAs);
        saveAs.addActionListener(this);
        add(bottom, "South");
        save.addActionListener(this);
        //jf.setAlwaysOnTop(true);
        jf.setContentPane(this);
        jf.setVisible(true);
    }

    public JFrame getFrame() {
        return jf;
    }

    public void setSong(SongFile song) {
        this.song = song;
        text = new JTextArea();
        text.setText(song.getOneLine());
    }

    public SongFile getSong() {
        return song;
    }

    private void saveSong(boolean saveAs) {
        String in = text.getText();
        if(saveAs) {
            JFileChooser jfc = new JFileChooser(new File("./Songs/"));
            int result = jfc.showSaveDialog(this);
            if(result == 0)
                try {
                    BufferedWriter bw = new BufferedWriter(new FileWriter((new StringBuilder()).append("./Songs/").append(jfc.getSelectedFile().getName()).toString()));
                    bw.write((new StringBuilder()).append(song).append("\n").toString());
                    bw.write(in);
                    bw.write((new StringBuilder()).append("\n\nCCLI Song No. ").append(number.getText()).toString());
                    bw.close();
                    song.readFile(jfc.getSelectedFile());
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
        } else {
            try {
                if(song.getFile()==null)
                    saveSong(true);
                if(!song.getFile().exists())
                    song.getFile().createNewFile();
                BufferedWriter bw = new BufferedWriter(new FileWriter(song.getFile()));
                bw.write((new StringBuilder()).append(song).append("\n").toString());
                bw.write(in);
                bw.write((new StringBuilder()).append("\n\nCCLI Song No. ").append(number.getText()).toString());
                bw.close();
                song.readFile(song.getFile());
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        song.setTitle(title.getText());
        if(e.getSource() == saveAs)
            saveSong(true);
        else
        if(e.getSource() == save)
            saveSong(false);
    }
}
